﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using QingzimaLogicModel;
using Jacaranda.Model;
using QingzimaDB;

namespace QingzimaScenario
{
    public class JiankangmimaScenario
    {
        public static JiankangmimaLM HuoquByID(int id)
        {
            return ConvertToLM(Database.SearchOne<JiankangmimaDB>(new AbstractProperty("ID") == id));
        }

        public static JiankangmimaLM HuoquByKey(string key)
        {
            return ConvertToLM(Database.SearchOne<JiankangmimaDB>(new AbstractProperty("Key") == key));
        }

        public static List<JiankangmimaLM> HuoquModels()
        {
            List<JiankangmimaLM> tempLMList = new List<JiankangmimaLM>();
            foreach (JiankangmimaDB dbModel in Database.Select<JiankangmimaDB>("Select * From JiankangmimaDB Order By CreatedTime Desc"))
            {
                tempLMList.Add(ConvertToLM(dbModel));
            }
            return tempLMList;
        }

        public static List<JiankangmimaLM> HuoquModelsByCreatedTime(int shuliang)
        {
            List<JiankangmimaLM> tempLMList = new List<JiankangmimaLM>();
            foreach (JiankangmimaDB dbModel in Database.Select<JiankangmimaDB>("Select top " + shuliang + " * From JiankangmimaDB Order By CreatedTime Desc"))
            {
                tempLMList.Add(ConvertToLM(dbModel));
            }
            return tempLMList;
        }

        public static void Tianjia(JiankangmimaLM JiankangmimaLM)
        {
            JiankangmimaLM.Save();
        }

        public static void Shanchu(string key)
        {
            Database.Delete<JiankangmimaDB>(new AbstractProperty("Key") == key);
        }

        private static JiankangmimaLM ConvertToLM(JiankangmimaDB dbModel)
        {
            return dbModel == null ? null : new JiankangmimaLM(dbModel);
        }
    }
}
